#!/bin/bash
# init script for the openQRM puppet-plugin
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#

OPENQRM_SERVER_BASE_DIR=$(pushd $(dirname $0)/../../../../.. > /dev/null; echo $PWD; popd > /dev/null)
. $OPENQRM_SERVER_BASE_DIR/openqrm/etc/openqrm-server.conf
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-server-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/include/openqrm-plugin-puppet-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/openqrm-plugin-puppet.conf
openqrm_server_get_config
export resource_openqrmserver=$OPENQRM_SERVER_IP_ADDRESS
export LANG=C
# define wget to use with https
if [ "$OPENQRM_WEB_PROTOCOL" == "https" ]; then
	WGET_NO_CERT_CHECK="--no-check-certificate"
fi


function openqrm_plugin_puppet_start() {
	echo "Starting the openQRM puppet-plugin"
	stop_puppet
	stop_puppet_master
	openqrm_chkconfig del puppet
	openqrm_chkconfig del puppetmaster
	start_puppet_master
	start_puppet
	# linking further install scripts
	mkdir -p $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/puppet/
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/install/openstack/openstack-all-in-one $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/puppet/openstack-all-in-one

	touch $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/puppet/.running
}


function openqrm_plugin_puppet_stop() {
	echo "Stopping the openQRM puppet-plugin"
	stop_puppet
	stop_puppet_master
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/puppet/.running
}

function openqrm_plugin_puppet_uninstall() {
	local USERNAME=$1
	local PASSWORD=$2
	if [ "$USERNAME" == "" ] && [ "$PASSWORD" == "" ]; then
		echo -n "puppet: Please give a username to uninstall the plugin : "
		read USERNAME
		echo
		echo -n "puppet: Please give the password for user $USERNAME : "
		read PASSWORD
		echo
	fi
	echo "Uninstalling the openQRM puppet-plugin"
	openqrm_plugin_puppet_stop

	# remove from cron
	rm -f /etc/cron.d/openqrm-puppetm-config
	if [ -x "/etc/init.d/cron" ]; then
		if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
			invoke-rc.d cron reload
		else
			/etc/init.d/cron reload
		fi
	elif [ -x /etc/init.d/crond ]; then
		if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
			invoke-rc.d crond reload
		else
			/etc/init.d/crond reload
		fi
	else
		echo "Could not find cron init script in /etc/init.d/. Please reload cron manually!"
	fi

	# removing the puppet tables
	if ! wget -q $WGET_NO_CERT_CHECK -O /dev/null --http-user=$USERNAME --http-password=$PASSWORD "$OPENQRM_WEB_PROTOCOL://$resource_openqrmserver/openqrm/base/plugins/puppet/puppet-action.php?puppet_command=uninstall"; then
		puppet_manager_log uninstall "ERROR: Could not drop the puppet-plugin tables in the database!"
		return 1
	fi
	# restore orgigin puppet dir
	if [ -d /etc/puppet.openqrm-puppet-plugin.bak ]; then
		rm -rf /etc/puppet
		cp -aR /etc/puppet.openqrm-puppet-plugin.bak /etc/puppet
	fi
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/puppet
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/boot-service-puppet.tgz
	rm -rf $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/puppet
	rm -rf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet
}

function openqrm_plugin_puppet_init() {
	local USERNAME=$1
	local PASSWORD=$2
	if [ "$USERNAME" == "" ] && [ "$PASSWORD" == "" ]; then
		echo -n "puppet: Please give a username to init the plugin : "
		read USERNAME
		echo
		echo -n "puppet: Please give the password for user $USERNAME : "
		read PASSWORD
		echo
	fi

	# the puppet plugin depends on dns working so it needs the dns plugin available+enabled
	# puppet autosigning needs a domain name. This we get from the dns-plugin config
	if [ ! -f $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/dns/etc/openqrm-plugin-dns.conf ]; then
		echo "ERROR: The openQRM puppet-plugin depends on the dns-plugin. Please enable the dns-plugin first!"
		openqrm_post_event 0 "puppet-plugin" 3 "puppet-plugin" "The openQRM puppet-plugin depends on the dns-plugin. Please enable the dns-plugin first!"
		return 1
	fi
	. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/dns/etc/openqrm-plugin-dns.conf

	CURRENTD=`pwd`
	echo "Initializing the openQRM puppet-plugin"
	# linking the web dir
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/web $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/puppet
	# link the boot-service
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/web/boot-service-puppet.tgz $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/boot-service-puppet.tgz
	# linking further install scripts
	mkdir -p $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/puppet/
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/install/openstack/openstack-all-in-one $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/puppet/openstack-all-in-one

	# creating the tables
	if ! wget -q $WGET_NO_CERT_CHECK -O /dev/null --http-user=$USERNAME --http-password=$PASSWORD "$OPENQRM_WEB_PROTOCOL://$resource_openqrmserver/openqrm/base/plugins/puppet/puppet-action.php?puppet_command=init&puppet_domain=$OPENQRM_SERVER_DOMAIN"; then
		puppet_manager_log init "ERROR: Could not create the puppet-plugin tables in the database!"
		return 1
	fi

	# creating the svn dir at /etc/puppet to store the puppet configuration
	# backup orgigin puppet dir first
	if [ ! -d /etc/puppet.openqrm-puppet-plugin.bak ]; then
		mv /etc/puppet /etc/puppet.openqrm-puppet-plugin.bak
	fi
	# create the svn dir
	rm -rf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet
	mkdir -p $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/default/files
	svnadmin create $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet
	svn import -q $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/default/ file://$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/ -m "initial import"
	# and import to /etc/puppet
	rm -rf /etc/puppet/*
	rm -rf /etc/puppet/.svn
	mkdir -p /etc/puppet
	cd /etc/puppet
	svn co -q file://$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/ .
	# also co the files for the web-dir
	rm -rf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/web/puppet/
	mkdir -p $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/web/puppet/
	cd $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/web/puppet/
	svn co -q file://$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/ .
	chmod -R 777 $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/web/puppet/
	# setup access through ssh+svn
	rm -f $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/conf/svnserve.conf
	echo "[general]" > $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/conf/svnserve.conf
	echo "anon-access = read" >> $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/conf/svnserve.conf
	echo "auth-access = write" >> $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/conf/svnserve.conf
	echo "realm = openQRM Puppet configuration" >> $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/conf/svnserve.conf
	echo "password-db = passwd" >> $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/conf/svnserve.conf
	chmod -R 777 $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/db
	cd $CURRENTD
	# configure autosigning
	echo "*.$OPENQRM_SERVER_DOMAIN" > /etc/puppet/autosign.conf
	# create cron job which will update the svn repo with files which were added by the web gui
	cat $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/cron.d/openqrm-puppetm-config.cron | \
		sed -e "s#@@OPENQRM_SERVER_BASE_DIR@@#$OPENQRM_SERVER_BASE_DIR#g" > /etc/cron.d/openqrm-puppetm-config
	if [ -x "/etc/init.d/cron" ]; then
		if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
			invoke-rc.d cron reload
		else
			/etc/init.d/cron reload
		fi
	elif [ -x /etc/init.d/crond ]; then
		if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
			invoke-rc.d crond reload
		else
			/etc/init.d/crond reload
		fi
	else
		echo "Could not find cron init script in /etc/init.d/. Please reload cron manually!"
	fi
	# create svn post-commit hook to immediately update the puppet configuration
	cat $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/svn-hook/post-commit.tmpl | \
		sed -e "s#@@OPENQRM_SERVER_BASE_DIR@@#$OPENQRM_SERVER_BASE_DIR#g" > $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/hooks/post-commit
	chmod 755 $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/puppet/etc/puppet/hooks/post-commit

	# eventually clean up old ssl dir before starting
	rm -rf /var/lib/puppet/ssl/*
	rm -rf /var/lib/puppet/yaml/node/*
	rm -rf /var/lib/puppet/yaml/facts/*

}







USER=$2
PASS=$3

case "$1" in
	start)
		openqrm_plugin_puppet_start
		;;
	stop)
		openqrm_plugin_puppet_stop
		;;
	restart)
		openqrm_plugin_puppet_stop
		sleep 1
		openqrm_plugin_puppet_start
		;;
	init)
		openqrm_plugin_puppet_init $USER $PASS
		;;
	uninstall)
		openqrm_plugin_puppet_uninstall $USER $PASS
		;;
	*)
		echo "Usage: $0 {start|stop|restart|init|uninstall}"
		exit 1

esac
exit $?




